package cn.ujn.medicalbackend.service.impl;


import cn.ujn.medicalbackend.common.ErrorCode;
import cn.ujn.medicalbackend.domain.Drug;
import cn.ujn.medicalbackend.exception.BusinessException;
import cn.ujn.medicalbackend.mapper.DrugMapper;
import cn.ujn.medicalbackend.request.DrugAddRequest;
import cn.ujn.medicalbackend.request.DrugDeleteRequest;
import cn.ujn.medicalbackend.request.DrugUpdateRequest;
import cn.ujn.medicalbackend.service.AccountService;
import cn.ujn.medicalbackend.service.DrugService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
* @author Drink
* @description 针对表【drug】的数据库操作Service实现
* @createDate 2024-10-24 15:46:45
*/
@Service
public class DrugServiceImpl extends ServiceImpl<DrugMapper, Drug>
    implements DrugService{

    @Autowired
    private DrugMapper drugMapper;

    @Autowired
    private AccountService accountService;

    @Override
    public Page<Drug> DrugPageTransfer(Page<Drug> tempPage) {
        return tempPage;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public long addDrug(DrugAddRequest drugAddRequest, HttpServletRequest request) {

        Drug drug = new Drug();
        BeanUtils.copyProperties(drugAddRequest, drug);
        System.out.println(drug.toString());
        Date now = new Date();
        drug.setCreatetime(now);
        drug.setUpdatetime(now);
        // 3.校验信息
        if (drug.getDrugName() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "药名不能为空");
        }
        boolean result;
        result = save(drug);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "添加药品失败");
        }

        return drug.getDrugId();
    }


    @Override
    public boolean updateDrug(DrugUpdateRequest drugUpdateRequest, HttpServletRequest request) {
        if (drugUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Date now = new Date();
        Drug drug =getById(drugUpdateRequest.getDrugId());
        if(drug==null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "该药品不存在，更新失败");
        }
        BeanUtils.copyProperties(drugUpdateRequest, drug);
        drug.setUpdatetime(now);

        boolean result =updateById(drug);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }

        return true;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDrug(DrugDeleteRequest drugDeleteRequest, HttpServletRequest request) {
        // 鉴权，只有管理员可创建
        Long drugId = drugDeleteRequest.getDrugId();

        if (drugId == null ) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "；医生id和账户名不能为空");
        }
        QueryWrapper<Drug> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("drug_id", drugId);
        long count = count(queryWrapper);
        if (count <= 0) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "没有该药品");
        }

        boolean result = removeById(drugId);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return true;

    }

}





